/*
 * Copyright 2008 Georgi Staykov
 * 
 * This file is part of pscoder.
 *
 * pscoder is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * pscoder is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with pscoder.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.gstaykov.pscoder.editor.completion;

import java.util.Arrays;
import java.util.Vector;

public class FileData {
	
	public String fileName;
	private String[] sourcedFiles = new String[0];
	private String[] functions = new String[0];
	private String[] variables = new String[0];
	
	public FileData(String fileName) {
		this.fileName = fileName;
	}
	
	public void setSourcedFiles(String[] data) {
		this.sourcedFiles = data;
	}
	
	public String[] getSourcedFiles() {
	    return this.sourcedFiles;
	}
	
	public void setFunctions(String[] data) {
	    this.functions = data;
	}
	
	public String[] getFunctions() {
	    return this.functions;
	}
	
	public void setVariables(String[] data) {
	    this.variables = data;
	}
	
	public String[] getVariables() {
	    return this.variables;
	}
	
	public String[] getMembers() {
		Vector<String> memberList = new Vector<String>();

		// First add the variables
		if (this.variables.length > 0) {
			memberList.addAll(Arrays.asList(this.variables));
		}
		
		// Then add the functions
		if (this.functions.length > 0) {
			memberList.addAll(Arrays.asList(this.functions));
		}
		
		// FIXME: [IN] Find a better way to cast to String[]?
		return memberList.toArray(new String[]{}); 
	}
	
	// TODO: Make the search wide instead of depth
	public String getFunctionOwner(String function, CompletionDictionary dict) {
	    String result = null;
	    
	    for (int i = 0; i < functions.length; i++) {
	        if (functions[i].startsWith(function + "(")) {
	            return this.fileName;
	        }
	    }
	    
	    for (int i = 0; i < sourcedFiles.length; i++) {
	        FileData data = dict.getFileData(dict.getFullFilenameForEnding(sourcedFiles[i]));
	        if (data != null) {
	            result = data.getFunctionOwner(function, dict);
	        }
	        if (result != null)
	            break;
	    }
	    
	    return result;
	}
}
